<%@ page import="cn.labbook.bluetooth.constant.AdminConstant" %>
<%@ page import="cn.labbook.bluetooth.common.TokenManager" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    %>
    <base href="<%=basePath%>">
    <title>电力管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" type="text/css" href="assets/css/admin/reset.css"/>
    <link rel="stylesheet" type="text/css" href="assets/plugins/layui/css/layui.css">
    <link rel="stylesheet" type="text/css" href="//at.alicdn.com/t/font_893379_qj8lfap4ylj.css"/>
</head>
<style>
    /*让徽章上下居中*/
    .layui-badge {
        top: 50%;
        transform: translateY(-50%);
    }
</style>
<body>
<div>
    <div class=" clearfix margin-15">
        <div class="layui-col-md12">
            <div class="layui-card-header">
                用户列表
            </div>
            <div class="layui-form layuiadmin-card-header-auto padding-tb-10 layui-card"
                 lay-filter="layadmin-userfront-formlist" id="searchForm">
                <div class="layui-form-item searchDiv" style="margin-bottom: 0">
                    <div class="layui-inline">
                        <label class="layui-form-label" style="text-align: left">用户名称</label>
                        <div class="layui-input-block">
                            <input type="text" placeholder="请输入用户名称" autocomplete="off"
                                   class="layui-input" id="adminName">
                        </div>
                    </div>
                    <%--<div class="layui-inline">--%>
                        <%--<label class="layui-form-label">用户状态</label>--%>
                        <%--<div class="layui-input-block ">--%>
                            <%--<select name="userStatus" id="adminStatus">--%>
                                <%--<option value="">请选择</option>--%>
                                <%--<option value=<%=AdminConstant.position.MANUFACTURERS%>>厂商</option>--%>
                                <%--<option value=<%=AdminConstant.position.DEALER%>>经销商</option>--%>
                                <%--<option value=<%=AdminConstant.position.LANDLORD%>>房东</option>--%>
                            <%--</select>--%>
                        <%--</div>--%>
                    <%--</div>--%>
                    <div class="layui-inline">
                        <button class="layui-btn layuiadmin-btn-useradmin" id="search"
                                lay-filter="LAY-user-front-search">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
            <div class="layui-tab-content">
                <div class="layui-btn-div">
                    <button class="layui-btn" data-type="addPm" id="addPm">新增</button>
                </div>
                <table class="layui-table" id="payPmList" lay-filter="payPmList"></table>
            </div>
        </div>
    </div>
</div>
<div id="addPmDiv" class="divHideLayer">
    <form class="layui-form" lay-filter="editForm" id="editForm">
        <table class="table_all">
            <tbody>
            <tr>
                <th style="width: 25%"><span class="red">*</span>用户姓名</th>
                <td style="width: 75%">
                    <input type="text" name="adminName" lay-verify="required" autocomplete="off"
                           class="layui-input layui-form-danger" placeholder="">
                </td>
            </tr>
            <tr id="accountNumber">
                <th><span class="red">*</span>账号</th>
                <td>
                    <input type="text" id="accountNumberInput" name="accountNumber" lay-verify="required" autocomplete="off"
                           class="layui-input layui-form-danger" placeholder="" readonly>
                </td>
            </tr>
            <tr id="password">
                <th><span class="red">*</span>密码</th>
                <td>
                    <input type="password" name="password" lay-verify="required" autocomplete="off"
                           class="layui-input layui-form-danger" placeholder="">
                </td>
            </tr>
            <tr>
                <th style="width: 25%"><span class="red">*</span>性别</th>
                <td style="width: 75%">
                    <select name="sex" id="sex" lay-verify="required">
                        <option value="">请选择</option>
                        <option value=<%=AdminConstant.SEX.W%>>女</option>
                        <option value=<%=AdminConstant.SEX.M%>>男</option>
                    </select>
                </td>
            </tr>
            <tr>
                <th style="width: 25%">固定电话</th>
                <td style="width: 75%">
                    <input type="text" name="telephone" lay-verify="telephone" autocomplete="off"
                           class="layui-input layui-form-danger" placeholder="">
                </td>
            </tr>
            <tr>
                <th style="width: 25%"><span class="red">*</span>移动电话</th>
                <td style="width: 75%">
                    <input type="text" name="mobilephone" lay-verify="mobilephone" autocomplete="off"
                           class="layui-input layui-form-danger" placeholder="">
                </td>
            </tr>
            <tr>
                <th style="width: 25%">邮箱</th>
                <td style="width: 75%">
                    <input type="text" name="email" autocomplete="off"
                           class="layui-input layui-form-danger" placeholder="">
                </td>
            </tr>
            <tr  id="adminId">
                <th style="width: 25%">固定序号</th>
                <td style="width: 75%">
                    <input type="text" name="adminId" autocomplete="off"
                           class="layui-input layui-form-danger" placeholder="" readonly>
                </td>
            </tr>
            <%--        --%>
            <%--<tr>--%>
            <%--<th style="width: 25%"><span class="red">*</span>服务费</th>--%>
            <%--<td style="width: 75%">--%>
            <%--<select name="sex" id="pay_price" lay-verify="required" lay-search>--%>
            <%--<option value="">请选择</option>--%>
            <%--<option value="100">100</option>--%>
            <%--<option value="200">200</option>--%>
            <%--<option value="300">200</option>--%>
            <%--</select>--%>
            <%--</td>--%>
            <%--</tr>--%>
            </tbody>
        </table>
        <div class="layui-form-item">
            <div class="submitForm">
                <button class="layui-btn" type="button" lay-submit="" lay-filter="editSubmit" id="cancell">提交</button>
                <button type="button" class="layui-btn layui-btn-primary" id="cancel" onclick="hideFun()">取消</button>
            </div>
        </div>
    </form>
</div>
<script type="text/html" id="barDemo">
    <a class="btn-white btn-warning layui-btn-xs" lay-event="edit">编 辑</a>
    <a class="btn-white btn-danger layui-btn-xs" lay-event="delete">删 除</a>
    <a class="btn-white btn-danger layui-btn-xs" lay-event="resetPwd">重置密码</a>
    <%--<shiro:hasPermission name="zkis">--%>
        <%--{{# if(d.adminStatus == <%=AdminConstant.position.DEALER%> && d.permission == <%=AdminConstant.permission.NO%>){ }}--%>
        <%--<a class="btn-white btn-success layui-btn-xs" lay-event="open">开通权限</a>--%>
        <%--{{# } }}--%>
        <%--{{# if(d.permission == <%=AdminConstant.permission.HAVE%> ){ }}--%>
        <%--<a class="btn-white btn-success layui-btn-xs" lay-event="close">关闭权限</a>--%>
        <%--{{# } }}--%>
    <%--</shiro:hasPermission>--%>
</script>
</body>
<script src="assets/plugins/Jquery/jquery.min.js"></script>
<script src="assets/plugins/layui/layui.all.js"></script>
<script src="assets/js/common.js"></script>
<script>
    var adminId = null;
    $(function () {
        layui.use(['table', 'form', 'laydate'], function () {
            var table = layui.table,
                form = layui.form;
            var data = [
                {type: 'numbers', title: '序号'},
                {field: 'adminId', title: 'ID', sort: false, align: 'center'},
                {field: 'adminName', title: '姓名', sort: false, align: 'center'},
                {
                    field: 'sex', title: '性别', sort: false, align: 'center', templet: function (d) {
                        if (d.sex == 1) return '<span class="layui-badge layui-bg-blue">男</span>'
                        else if (d.sex == 0) return '<span class="layui-badge layui-bg-red">女</span>'
                        else return '<span class="layui-badge layui-bg-cyan">未知</span>'
                    }
                },
                {field: 'telephone', title: '固定电话', sort: false, align: 'center'},
                {field: 'mobilephone', title: '移动电话', sort: false, align: 'center'},
                {field: 'monthtotal', title: '当月出货量', sort: false, align: 'center'},
                {field: 'total', title: '总出货量', sort: false, align: 'center'},
                {field: 'email', title: '邮箱', sort: false, align: 'center'},
                {
                    field: 'adminStatus', title: '用户状态', sort: false, align: 'center', templet: function (d) {
                        if (d.adminStatus == <%=AdminConstant.position.DEALER%>) return '<span class="layui-badge">经销商</span>'
                        else if (d.adminStatus == <%=AdminConstant.position.MANUFACTURERS%>) return '<span class="layui-badge layui-bg-green">厂商</span>'
                        else if (d.adminStatus == <%=AdminConstant.position.LANDLORD%>) return '<span class="layui-badge layui-bg-blue">房东</span>'
                        else return '<span class="layui-badge layui-bg-cyan">未知</span>'
                    }
                },
                {title: '操作',width:200, sort: false, align: 'center', toolbar: '#barDemo'}
            ];
            table.render(
                renderTable("#payPmList", 'admin/page', data)
            );

            table.on('tool(payPmList)', function (obj) {
                var data = obj.data;
                adminId = data.adminId;
                if (obj.event === "edit") {
                    // $("#accountNumber").hide();
                    $("#password").hide();
                    layer.open({
                        type: 1,
                        title: "编辑",
                        btnAlign: "c",
                        offset: ['150px'],
                        area: ['500px', '500px'],
                        content: $("#addPmDiv"),
                        success: function () {
                            form.val('editForm', {
                                "adminName": data.adminName,
                                "adminStatus": data.adminStatus,
                                "accountNumber": data.accountNumber,
                                "sex": data.sex,
                                "telephone": data.telephone,
                                "mobilephone": data.mobilephone,
                                "email": data.email,
                                "adminId":data.adminId
                            })
                        },
                        end: function (i) {
                            layer.close(i);
                            hideWithReset("#addPmDiv", "#editForm")
                        }
                    });
                }
                if (obj.event === "resetPwd") {
                    layer.confirm('确定重置该用户密码？', function (index) {
                        postAjaxReq("admin/reset/password", {"adminId": data.adminId}, function (res) {
                            if (res.code == 1) {
                                obj.del();
                                layer.msg("操作成功")
                                table.reload('payPmList')
                                adminId = null
                            } else {
                                layer.msg(res.msg)
                            }

                        });
                        layer.close(index);
                    })
                }
                if (obj.event === "delete") {
                    layer.confirm('确定删除该用户？', function (index) {
                        postAjaxReq("admin/delete", {"adminId": data.adminId}, function (res) {
                            if (res.code == 1) {
                                obj.del();
                                layer.msg("操作成功")
                                table.reload('payPmList')
                                adminId = null
                            } else {
                                layer.msg(res.msg)
                            }

                        });
                        layer.close(index);
                    })
                }
                if (obj.event === "open") {
                    layer.confirm('确定开通该经销商服务费设置权限？', function (index) {
                        postAjaxReq("admin/edit", {"adminId": data.adminId, "permission": 1}, function (res) {
                            if (res.code == 1) {
                                obj.del();
                                layer.msg("操作成功")
                                table.reload('payPmList')
                                adminId = null
                            } else {
                                layer.msg(res.msg)
                            }

                        });
                        layer.close(index);
                    })
                }
                if (obj.event === "close") {
                    layer.confirm('确定关闭该经销商服务费设置权限？', function (index) {
                        postAjaxReq("admin/edit", {"adminId": data.adminId, "permission": 0}, function (res) {
                            if (res.code == 1) {
                                obj.del();
                                layer.msg("操作成功")
                                table.reload('payPmList')
                                adminId = null
                            } else {
                                layer.msg(res.msg)
                            }

                        });
                        layer.close(index);
                    })
                }
            });
            form.verify({
                mobilephone: [/^[1][3,4,5,7,8][0-9]{9}$/, '请正确输入移动电话'],
                telephone: function (value) {
                    var check = /^(0[0-9]{2,3}\-)([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/;
                    if ((value != null && value != '' && value.trim() != '') && !check.test(value)) {
                        return '请正确输入固定电话'
                    }
                }
            });
            $("#addPm").click(function () {
                $("#accountNumber").show();
                $("#accountNumberInput").attr("readonly",false);
                $("#password").show();
                arrayAjaxReq("admin/get/adminId", data, function (res) {
                    if(res){
                        if(res.data==null){
                            $("#adminId").hide();
                        }
                        console.log(res.data)
                        var admimId =res.data
                        layer.open({
                            type: 1,
                            title: "",
                            btnAlign: "c",
                            offset: ['50px'],
                            area: ['500px', '600px'],
                            content: $("#addPmDiv"),
                            success: function () {
                                form.val('editForm', {
                                    "adminId":admimId
                                })
                            },
                            end: function (i) {
                                layer.close(i);
                                hideWithReset("#addPmDiv", "#editForm")
                            }
                        });
                    }
                });
            });
            form.on('submit(editSubmit)', function (obj) {
                var url = null;
                var data = obj.field
                if (!isEmpty(adminId)) {
                    data.adminId = adminId
                    url = "admin/edit"
                } else
                    url = "admin/add"
                postAjaxReq(url, data, function (res) {

                    if (res.code == 1) {
                        layer.closeAll();
                        table.reload('payPmList');
                        layer.msg("保存成功")
                        adminId = null
                    } else {
                        layer.msg(res.msg)
                    }
                })
            });

            $("#search").click(function () {
                var adminName = $("#adminName").val(),
                    adminStatus = $("#adminStatus").val();

                table.reload('payPmList', {
                    page: {curr: 1},
                    where: {
                        adminName: adminName,
                        adminStatus: adminStatus
                    }
                })
            });
        });
    })

    $("#cancel").on("click", function () {
        layer.closeAll()
    });

    function hideFun() {
        layer.closeAll();
        adminId = null
    }

    function hideWithReset(hide, reset) {
        $(hide).hide()
        $(reset)[0].reset()
    }
</script>
</body>
</html>
